package com.jxzyk.model.users;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.alibaba.fastjson.JSONObject;
import com.core.ui.activity.BaseActivity;
import com.core.util.T;
import com.core.util.UiTools;
import com.jxzyk.R;
import com.jxzyk.http.BaseRequest;
import com.jxzyk.http.BaseRequest.OnDialogListener;
import com.jxzyk.http.req.RegisterRequest;
import com.jxzyk.view.TitleLayout;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

@ContentView(R.layout.act_register)
public class RegisterActivity extends BaseActivity {
	@ViewInject(R.id.title)
	TitleLayout title;

	@ViewInject(R.id.et_reg_phone)
	EditText et_phone;
	@ViewInject(R.id.et_reg_pwd)
	EditText et_pwd;

	@OnClick(value = { R.id.btn_reg_sumbit })
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_reg_sumbit:
			RegisterRequest request = new RegisterRequest(RegisterActivity.this);
			String phone = UiTools.getString(et_phone);
			if (!verifyPhone(phone)) {
				return;
			}
			String pwd = UiTools.getString(et_pwd);
			if (!UiTools.Empty(pwd)) {
				T.toast("密码输入有误");
				return;
			}

			request.setParams(phone, pwd);
			request.setOnDialogListener(new OnDialogListener() {

				@Override
				public void onSuccess(JSONObject json) {
					T.toast("注册成功");
					finish();
				}

				@Override
				public void onFailure(String msg) {
					T.toast(msg);
				}
			});
			request.show("正在注册...");
			break;
		default:
			break;
		}
	}

	public boolean verifyPhone(String phone) {
		if (phone.length() == 11
				&& phone.matches("(13[0-9]|15[6-9]|15[0-3]|18[0-9]|145|147|17[6-8])[0-9]{8}$"))
			return true;
		T.toast("手机号格式有误");
		return false;
	}

	@Override
	public void initData(Bundle bundle) {

	}

	@Override
	public void initUi() {
		title.setTtile("注册");
		title.setOnLeftListener(R.drawable.ic_chevron_left_white_48dp,
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						finish();
					}
				});
		// setSupportActionBar(mToolbar);
		// CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout)
		// findViewById(R.id.collapsing_toolbar);
		// collapsingToolbar.setTitle("我的课程");

	}

	// public void checkin(View view) {
	// Snackbar.make(view, "checkin success!", Snackbar.LENGTH_SHORT).show();
	// }
}
